// =======================================================================
// 
//       Filename:  element.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  20.04.2010 00:50:02
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================

#include "element.hpp"
#include "../types/object.hpp"

using namespace std;

namespace webplodder {
    using namespace types;

    namespace templatefile {

        Element::Element(File& parent, Element::Type type)
            : Object(Object::TEMPLATE_ELEMENT), 
            mParent(&parent), 
            mType(type), mLine(-1),
            mCache(false), mIndex(0)
        {
        }

        Element::~Element() {
        }

        Element::Type Element::elementType() const

        {
            return mType;
        }

        int Element::line() const

        {
            return mLine;
        }

        void Element::setLine(int line)

        {
            mLine = line;
        }

        void Element::printTo(ostream& output)
        {
            char ch;

            while((*this) >> (ch))
                output << ch;
        }

        bool Element::operator>>(char& ch)

        {
            if(mCache) {
                if(mIndex < mBuffer.size()) {

                    ch = mBuffer[mIndex];
                    ++mIndex;
                    return true;
                }
                else {
                    mIndex = 0;
                    return false;
                }
            }
            else {
                return getChar(ch);
            }
        }

        Element& Element::operator<<(char ch) {
            if(!mCache)
                mCache = true;

            mBuffer.push_back(ch);
            return *this;
        }
    }
}

bool operator<<(char& ch, webplodder::templatefile::Element& element)
   
{
    return element >> ch;
}

webplodder::templatefile::Element& operator>>(char ch, 
        webplodder::templatefile::Element& element)
{
    return element << ch;
}
